package com.epfresh.views.stick;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Scroller;

import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.widget.list.LoadMoreListView;

/**
 * Created by wzh on 16-11-12.
 */

public class StickLayout extends RelativeLayout {
    private String TAG = "StickLayout";
    private int mLastY = 0;  //最后的点
    private int mLastX = 0;  //最后的点
    private static int mNeedDistance;   // 需要滑动的距离
//    private static int mMinHight; //最小高度
//    private static int mOrignHight; //原始的高度

    private int mCurrentDistance = 0;  //当前的距离
    private float mRate = 1.2f;  //距离与目标距离的变化率 mRate=mCurrentDistance/mNeedDistance

    private RelativeLayout vHeader;
    private View vContent;

    protected CheckCanDoSticky checkCanDoSticky;

    private VelocityTracker velocityTracker;
    private Scroller scroller;

    private int  mTouchSlop = 0;

    public StickLayout(Context context) {
        super(context);
    }

    public StickLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StickLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CheckCanDoSticky getCheckCanDoSticky() {
        return checkCanDoSticky;
    }

    public void setCheckCanDoSticky(CheckCanDoSticky checkCanDoSticky) {
        this.checkCanDoSticky = checkCanDoSticky;
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() != 2 && getChildAt(0) instanceof RelativeLayout) {
            throw new IllegalStateException("stickHeader must have 2 elements ");
        }
        vHeader = (RelativeLayout) getChildAt(0);
        vContent = getChildAt(1);

//        mOrignHight = LocalDisplay.dp2px(142);
//        mCurrentDistance = LocalDisplay.dp2px(160);
        mNeedDistance = LocalDisplay.dp2px(95);
//        mMinHight = LocalDisplay.dp2px(47);
        scroller = new Scroller(getContext());

        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

    }

    public void initScroll() {

    }

//    @Override
//    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//
//        int top =  vHeader.getTop() - dy;
//        int right = vHeader.getMeasuredWidth();
//        int bottom = top + vHeader.getMeasuredHeight();
//        vHeader.layout(0,top,right,bottom);
//
//
//        vContent.layout(0,bottom,right,getMeasuredHeight()+right-LocalDisplay.dp2px(47));
//
//        Log.v("layout", "111111111===========================");
//        Log.e("layout", "dy:" + dy);
//        Log.e("layout", "top:" + top);
//        Log.e("layout", "right:" + right);
//        Log.e("layout", "bottom:" + bottom);
//        Log.e("layout", "getMeasuredHeight:" + getMeasuredHeight());
//        Log.e("layout", "vHeader.getMeasuredHeight():" + vHeader.getMeasuredHeight());
//        Log.i("layout", "222222222===========================");
//    }

    //    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {

//        velocityTracker.addMovement(ev);
//        switch (ev.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//
//                if (!scroller.isFinished()) {
//                    scroller.abortAnimation();
//                }
//                mLastY = (int) ev.getY();
//                mLastX = (int) ev.getX();
//                return super.dispatchTouchEvent(ev); //传递事件 例如可以用来子view的点击事件等
//            case MotionEvent.ACTION_MOVE:
//                int y = (int) ev.getY();
//                int x = (int) ev.getX();
//                int dy = mLastY - y;
//                int dx = mLastX - x;
//
//                if (Math.abs(dy) < Math.abs(dx)) {
//                    return super.dispatchTouchEvent(ev);  //传递事件
//                }
//                mLastY = y;
////                scrollBy(0,dy);
//                //改变布局
////                if (dy > 0) {
//                boolean b =checkTheLayout(dy);
//                Log.v("margin", "b1=" + b);
//                    return b;
////                }
////                return super.dispatchTouchEvent(ev);
//            case MotionEvent.ACTION_UP:
//                velocityTracker.computeCurrentVelocity(5);
//                Log.e("tracker", "trackerY=" + velocityTracker.getYVelocity());
//
////                scroller.fling(0, mLastY, 0, (int) velocityTracker.getYVelocity(), 0, 0, 0, 99999999);
//
////                velocityTracker.recycle();
////                velocityTracker = null;
//                return super.dispatchTouchEvent(ev);
//        }
//        boolean b = super.onInterceptTouchEvent(ev);
//        Log.v("margin", "b1=" + b);
//        return true ;
//    }

    public boolean dispatchTouchEventSupper(MotionEvent e) {
        return super.dispatchTouchEvent(e);
    }
    int dy = 0;

    boolean isOnVertically = false;
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (velocityTracker == null) {
//            velocityTracker = VelocityTracker.obtain();
//        }
//        Log.e("MotionEvent", "ev.getAction()=" + ev.getAction());
//        Log.i("MotionEvent", "isOnVertically=" + isOnVertically);
//        Log.i("MotionEvent", "super.dispatchTouchEvent(ev)=" + super.dispatchTouchEvent(ev));
//        switch (ev.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//
////                if (!scroller.isFinished()) {
////                    scroller.abortAnimation();
////                }
//                mLastY = (int) ev.getY();
//                mLastX = (int) ev.getX();
//
//
////                if (isOnVertically){
////                    return true;
////                }
////                dispatchTouchEventSupper(ev);
//                return dispatchTouchEventSupper(ev);
////                return super.dispatchTouchEvent(ev); //传递事件 例如可以用来子view的点击事件等
//            case MotionEvent.ACTION_MOVE:
//                int y = (int) ev.getY();
//                int x = (int) ev.getX();
//                int dy = mLastY - y;
//                int dx = mLastX - x;
//
//                if (!isOnVertically && Math.abs(dy) < Math.abs(dx)) {
//                    return super.dispatchTouchEvent(ev);  //传递事件
//                }
//                mLastY = y;
////                scrollBy(0,dy);
//                //改变布局
////                if (dy > 0) {
////                if (Math.abs(dy) > mTouchSlop) {
//                boolean b = changeTheLayout((int) (dy ));
//
//                    isOnVertically = b;
//
//                this.dy = dy;
////                requestLayout();
//                    Log.v("margin", "b=" + b);
////                }
//                return b;
////                }
////                return super.dispatchTouchEvent(ev);
//            case MotionEvent.ACTION_UP:
////                velocityTracker.computeCurrentVelocity(5);
//                Log.e("tracker", "trackerY=" + velocityTracker.getYVelocity());
//                isOnVertically = false;
////                scroller.fling(0, mLastY, 0, (int) velocityTracker.getYVelocity(), 0, 0, 0, 99999999);
//
////                velocityTracker.recycle();
////                velocityTracker = null;
//                return dispatchTouchEventSupper(ev);
//        }
//        return dispatchTouchEventSupper(ev);
//    }

    private boolean changeTheLayout(int dy) {
        if (dy == 0){
            return false;
        }
        //dy>0上推
        RelativeLayout.LayoutParams lp = (LayoutParams) vHeader.getLayoutParams();

//        if (Math.abs(dy) <= mTouchSlop){
//            return false;
//        }
        int top = lp.topMargin - dy;
        Log.e("margin", "dy=" + dy);
        Log.e("margin", "lp.topMargin1=" + lp.topMargin);
        Log.e("margin", "mNeedDistance=" + mNeedDistance);
//        //top <0,上推
//        if (top < 0 && -top < mNeedDistance) {
//            lp.topMargin = top;
//        } else if (top < 0 && -top >= mNeedDistance) {
//            lp.topMargin = -mNeedDistance;//滑动到顶部
//        } else if (top >= 0) {
//            lp.topMargin = 0;//滑动到底部
//        }
        if (dy > 0 && lp.topMargin <= -mNeedDistance){
            return false;
        }
        if (dy < 0 && lp.topMargin >= 0){
            return false;
        }

        top = getHeaderMargin(top);
        if (top == lp.topMargin) {
            return false;
        }
        lp.topMargin = top;


        vHeader.setLayoutParams(lp);
        requestLayout();
        Log.e("margin", "lp.topMargin2=" + lp.topMargin);

//        if (lp.topMargin == 0 || lp.topMargin == -mNeedDistance) {
//            return false;
//        }

        return true;
    }
    private boolean checkTheLayout(int dy) {
        //dy>0上推
        RelativeLayout.LayoutParams lp = (LayoutParams) vHeader.getLayoutParams();

        if (Math.abs(dy) <= mTouchSlop){
            return false;
        }
        int top = lp.topMargin - dy;
        Log.e("margin", "dy111=" + dy);
        Log.e("margin", "lp.topMargin111=" + lp.topMargin);
        Log.e("margin", "mNeedDistance111=" + mNeedDistance);
//        //top <0,上推
//        if (top < 0 && -top < mNeedDistance) {
//            lp.topMargin = top;
//        } else if (top < 0 && -top >= mNeedDistance) {
//            lp.topMargin = -mNeedDistance;//滑动到顶部
//        } else if (top >= 0) {
//            lp.topMargin = 0;//滑动到底部
//        }

        top = getHeaderMargin(top);
        if (top == lp.topMargin) {
            return false;
        }

        return true;
    }


    public int getHeaderMargin(int top) {
        if (top < 0 && -top < mNeedDistance) {
            return top;
        } else if (top < 0 && -top >= mNeedDistance) {
            return -mNeedDistance;//滑动到顶部
        } else if (top >= 0) {
            return 0;//滑动到底部
        }

        return 0;
    }

    public int getHeaderMarginTop() {
        RelativeLayout.LayoutParams lp = (LayoutParams) vHeader.getLayoutParams();
        return lp.topMargin;
    }

    public void setHeaderMarginTop(int top) {
        RelativeLayout.LayoutParams lp = (LayoutParams) vHeader.getLayoutParams();
        lp.topMargin = top;
        vHeader.setLayoutParams(lp);
    }


}
